package com.yukh.test.javabase.collection.map;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@SuppressWarnings("all")
public class HashMapDemo {
	class Name {
		private String first;
		private String last;

		public Name() {
		}

		public Name(String first, String last) {
			this.first = first;
			this.last = last;
		}

		public boolean equals(Object o) {
			if (this == o) {
				return true;
			}

			if (o.getClass() == Name.class) {
				Name n = (Name) o;
				return n.first.equals(first) && n.last.equals(last);
			}
			return false;
		}

		/*public int hashCode() {
			return first.hashCode()+last.hashCode();
		}
		public String toString() {
			return "Name[first=" + first + ", last=" + last + "]";
		}*/
	}
	
	private static void compareIterator(){
		long start = new Date().getTime();
		Map map = new HashMap(390000);
		long size = 390000l;
		for(int i=0; i<size; i++)
			map.put(String.valueOf(i), String.valueOf(i));
		
		long one = new Date().getTime();
		System.out.println("one:"+ (one - start));
		System.out.println(one);
		Iterator it = map.keySet().iterator();
		while(it.hasNext()){
			String key = (String)it.next();
			String value = (String)map.get(key);
		}
		long two = new Date().getTime();
		System.out.println("two:"+ (two - one));
		System.out.println(two);
		Iterator it1 = map.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry en = (Map.Entry)it.next();
			String value = (String)en.getValue();
		}
		long three = new Date().getTime();
		System.out.println("three:"+ (three - two));
		System.out.println(three);
	}
	
	private static int oldHash(int h) {
        h += ~(h << 9);
        h ^=  (h >>> 14);
        h +=  (h << 4);
        h ^=  (h >>> 10);
        return h;
    }
	
	private static int newHash(int h) {
	    h ^= (h >>> 20) ^ (h >>> 12);
	    return h ^ (h >>> 7) ^ (h >>> 4);
	}
	
	private static int hash(int h) {
		return false ? newHash(h) : oldHash(h);
    }
	
	private static void show(Map map){
		Iterator it = map.entrySet().iterator();
		while(it.hasNext()){
		  Map.Entry me = (Map.Entry)it.next();
		  String key = (String)me.getKey();
		  String value = (String)me.getValue();
		  System.out.println(key +"=" + value);
		}
	}
	
	public static void main(String[] args) {
		/*HashMapDemo1 hmd = new HashMapDemo1();
		Map map = new HashMap(1, 2);
		map.put("1", hmd.new Name("1", "2"));
		map.put("2", hmd.new Name("2", "3"));
		System.out.println(map.size());
		System.out.println(map.containsValue(hmd.new Name("1", "2")));*/
		
		Map map = new HashMap();
		map.put("1", "22");
		map.put("2", "22");
		
		show(map);
	}
}
